import com.alibaba.fastjson.JSON;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class FeigeTest {
	private String secret ="aba4758dbd5d72b2e037ed6f213223c8";
	private String app_key ="a095427168b2a1f81d84036836e7a3fd";
	private String template_id ="xAI6_jitx65PUMywPu-CNV1O48zMcE94dRR0hVH3GPc";

	public static void main(String[] args) {
		FeigeTest feigeTest = new FeigeTest();
		String msg = feigeTest.getMessString("告警测试","备注内容测试");
		feigeTest.sendMsg(msg);
	}


	public String getMessString(String content, String remark) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		MessageEntity messageEntity = new MessageEntity();
		messageEntity.setFirst(new ChildEntity("安防监控"));
		messageEntity.setContent(new ChildEntity(content));
		messageEntity.setRemark(new ChildEntity(remark));
		messageEntity.setOccurtime(new ChildEntity(sdf.format(new Date())));
		return JSON.toJSONString(messageEntity);
	}

	public void sendMsg(String data) {
		// 创建一个httpclient对象
		CloseableHttpClient client = HttpClients.createDefault();
		// 创建一个post对象
		HttpPost post = new HttpPost("https://u.ifeige.cn/api/message/send");
		// 创建一个Entity，模拟表单数据
		List<NameValuePair> formList = new ArrayList<NameValuePair>();
		// 添加表单数据

//        {"secret":"aba4758dbd5d72b2e037ed6f213223c8","app_key":"7170b0236ce96c6a6b0b7e4a544eb30c","template_id":"xAI6_jitx65PUMywPu-CNV1O48zMcE94dRR0hVH3GPc","data":{"first":{"value":"安防监控","color":"#173177"},"content":{"value":"检测到温度异常","color":"#173177"},"occurtime":{"value":"2020年5月3日17:46:29","color":"#173177"},"remark":{"value":" 环境温度56，火焰传感器检测到火源","color":"#173177"}}}

		formList.add(new BasicNameValuePair("secret", secret));
		formList.add(new BasicNameValuePair("app_key", app_key));
		formList.add(new BasicNameValuePair("template_id", template_id));
		formList.add(new BasicNameValuePair("data", data));

		try {
			// 包装成一个Entity对象
			StringEntity entity = new UrlEncodedFormEntity(formList, "utf-8");
			// 设置请求的内容
			post.setEntity(entity);
			// 设置请求的报文头部的编码
			// post.setHeader(new BasicHeader("Content-Type",
			// "application/x-www-form-urlencoded; charset=utf-8"));
			// 设置期望服务端返回的编码
			// post.setHeader(new BasicHeader("Accept",
			// "text/plain;charset=utf-8"));
			// 执行post请求
			CloseableHttpResponse response = client.execute(post);
			// 获取响应码
			int statusCode = response.getStatusLine().getStatusCode();
			if (statusCode == 200) {
				// 获取数据
				String resStr = EntityUtils.toString(response.getEntity());
				// 输出
				System.out.println(resStr);
			} else {
				// 输出
				System.out.println(statusCode);
			}
		} catch (IOException e) {

		}

	}

}
